/**
 * Created by jw on 2016/11/2.
 */
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var util = require('../utils/util');
var dataConfig = require('../config/data');
var EventEmitter = require('events').EventEmitter;
EventEmitter.defaultMaxListeners = dataConfig.defaultMaxListeners;

var distDir = dataConfig.dirs.dist;
var devDir = dataConfig.dirs.dev;
var dev = {};//开发版本
var dist = {};//发布版本

//公共模块
var browCommonPluginJsArr = dataConfig.dirs.browser.pluginsJsArr;
var toolJsArr = dataConfig.dirs.common.toolJsArr;
var initJsArr = dataConfig.dirs.common.initJsArr;
var t3dJsArr = dataConfig.dirs.common.t3dJsArr;
var t3dExJsArr = dataConfig.dirs.common.t3dExJsArr;
var chromePluginJsArr = dataConfig.dirs.browser.chromePluginJsArr;
var logicUniverseJsArr = dataConfig.dirs.node.logicUniverseJsArr;
var serverJsArr = dataConfig.dirs.common.serverJsArr;
var threeDJsArr = dataConfig.dirs.node.threeDJsArr;
var u3dJsArr = dataConfig.dirs.common.u3dJsArr;
var configJsArr = dataConfig.dirs.common.configJsArr;
var uwebUtilJsArr = dataConfig.dirs.browser.uwebUtilJsArr;
var uwebHomeJsArr = dataConfig.dirs.browser.uwebHomeJsArr;
var logicUniverseFor3DJsArr = dataConfig.dirs.node.logicUniverseFor3DJsArr;

var frontendConfigManagerJsArr = dataConfig.dirs.node.frontendConfigManagerJsArr;

var homePluginJsArr = dataConfig.dirs.home.pluginJsArr;
var homePluginCssArr = dataConfig.dirs.home.cssArr;

var gisCommonJsArr = dataConfig.dirs.node.uGeoJsArr;

var homeAppJsArr = dataConfig.dirs.home.appJsArr;
var gisPluginJsArr = homePluginJsArr;

var gisPluginCssArr = homePluginCssArr;

var gisAppJsArr = dataConfig.dirs.home.gisAppJsArr;

var threeDConfigPluginJsArr = dataConfig.dirs.threeDConfig.pluginJsArr;

var threeDConfigAppJsArr = dataConfig.dirs.threeDConfig.appJsArr;

//by lcj
var threeDConfigAppJsArrForVisibleconfig = dataConfig.dirs.threeDConfig.visibleconfigJsArr;

var threeDConfigCssArr = dataConfig.dirs.threeDConfig.cssArr;

var portShowPluginsJsArr = dataConfig.dirs.portShow.pluginsJsArr;
var portShowAppJsArr = dataConfig.dirs.portShow.appJsArr;
var portShowCssArr = dataConfig.dirs.portShow.cssArr;

var viewPointSetPluginsJsArr = dataConfig.dirs.viewPointSet.pluginsJsArr;
var viewPointSetAppJsArr = dataConfig.dirs.viewPointSet.appJsArr;
var viewPointSetCssArr = dataConfig.dirs.viewPointSet.cssArr;

var listPluginsJsArr = dataConfig.dirs.list.pluginsJsArr;
var listPluginsCssArr = dataConfig.dirs.list.cssArr;
var listAppJsArr = dataConfig.dirs.list.appJsArr;

var managerPluginsJsArr = dataConfig.dirs.manager.pluginsJsArr;
var managerPluginsCssArr = dataConfig.dirs.manager.cssArr;
var managerAppJsArr = dataConfig.dirs.manager.appJsArr;

var resourcesPluginsJsArr = dataConfig.dirs.resources.pluginsJsArr;
var resourcesPluginsCssArr = dataConfig.dirs.resources.cssArr;
var resourcesAppJsArr = dataConfig.dirs.resources.appJsArr;

var controlPluginsJsArr = dataConfig.dirs.control.pluginsJsArr;
var controlPluginsCssArr = dataConfig.dirs.control.cssArr;
var controlAppJsArr = dataConfig.dirs.control.appJsArr;

var headCssArr = dataConfig.dirs.head.cssArr;
var headJsArr = dataConfig.dirs.head.jsArr;

var givmappingPluginsCssArr = dataConfig.dirs.givTMapping.cssArr;
var givmappingPluginsJsArr = dataConfig.dirs.givTMapping.pluginsJsArr;
var givmappingAppJsArr = dataConfig.dirs.givTMapping.appJsArr;

var givListPluginsCssArr = dataConfig.dirs.givList.cssArr;
var givListPluginsJsArr = dataConfig.dirs.givList.pluginsJsArr;
var givListAppJsArr = dataConfig.dirs.givList.appJsArr;

//状态机公用文件
var stateMachineCoreMinJsArr = dataConfig.dirs.stateMachine.coreMinJsArr;
var stateMachineExtendJsArr = dataConfig.dirs.stateMachine.jsArr;

dev.getInjectViewsOptions = function (destDir, options) {
  var defaultOptions = {
    pluginsJsDir: devDir,
    appDir: devDir,
    cssDir: devDir,
    destDir: devDir + destDir,

    //转换函数
    transformJsFun: dev.transformJsFun_views,
    transformCssFun: dev.transformCssFun_views
  };
  return util.combine(defaultOptions, options);
};

dist.getInjectViewsOptions = function (destDir, options) {
  var defaultOptions = {
    pluginsJsDir: distDir,
    appDir: distDir,
    cssDir: distDir,
    destDir: distDir + destDir,

    //转换函数
    transformJsFun: dist.transformJsFun_views,
    transformCssFun: dist.transformCssFun_views
  };
  return util.combine(defaultOptions, options);
};

/**
 * 获取js引用的字符串
 * @param filePath 文件路径
 * @param index 要截取路径字符串的起始值
 * @param addStr 路径前缀
 */
var getJsStr = function (filePath, index, addStr) {
  // console.log(filePath);
  addStr = addStr || '';
  return '<script src="' + addStr + filePath.substr(index) + '">' + '<' + '/script>';
};

/**
 * 获取CSS引用的字符串
 * @author jw
 * @date 2016-11-08
 * @param filePath 文件路径
 * @param index 要截取路径字符串的起始值
 * @param addStr 路径前缀
 */
var getCssStr = function (filePath, index, addStr) {
  // console.log(filePath);
  addStr = addStr || '';
  return '<link rel="stylesheet" type="text/css" href="' + addStr + filePath.substr(index) + '"/>';
};

/**
 * 替换引用的js/css到html
 * @param {String} html 要注入的Html名称
 * @param {Object} config 配置信息
 */
var injectFun = function (html, config) {
  var temp = gulp.src([html], {cwd: './src/entry/'})
    .pipe($.inject(gulp.src(config.commonPlugins, {read: false, cwd: config.pluginsJsDir}), {
      relative: true,
      starttag: '<!-- start:common-pluginsJs -->',
      endtag: '<!-- end:common-pluginsJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(
      $.if(config.plugins && config.plugins.length > 0, config.plugins, browCommonPluginJsArr) //没有设置时，就取公用的插件
      , {read: false, cwd: config.pluginsJsDir}), {
      relative: true,
      starttag: '<!-- start:pluginsJs -->',
      endtag: '<!-- end:pluginsJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.tool, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:toolJs -->',
      endtag: '<!-- end:toolJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.t3d, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:t3dJs -->',
      endtag: '<!-- end:t3dJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.init, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:initJs -->',
      endtag: '<!-- end:initJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.server, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:serverJs -->',
      endtag: '<!-- end:serverJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.frontendConfigManager, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:frontendConfigManagerJs -->',
      endtag: '<!-- end:frontendConfigManagerJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.threeD, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:3dJs -->',
      endtag: '<!-- end:3dJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.t3dExJsArr, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:t3dExJs -->',
      endtag: '<!-- end:t3dExJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.chromePlugin, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:chromePluginJs -->',
      endtag: '<!-- end:chromePluginJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.u3d, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:u3dJs -->',
      endtag: '<!-- end:u3dJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.logicUniverse, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:logicUniverseJs -->',
      endtag: '<!-- end:logicUniverseJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.stateMachineCoreMinJs, {read: false, cwd: config.stateMachineJsDir}), {
      relative: true,
      starttag: '<!-- start:stateMachineCoreMinJs -->',
      endtag: '<!-- end:stateMachineCoreMinJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.stateMachineExtend, {read: false, cwd: config.stateMachineJsDir}), {
      relative: true,
      starttag: '<!-- start:stateMachineExtend -->',
      endtag: '<!-- end:stateMachineExtend -->',
      transform: config.transformJsFun
    }))
    // .pipe($.inject(gulp.src(config.stateMachineIdeJs, {read: false, cwd: config.stateMachineJsDir}), {
    //   relative: true,
    //   starttag: "<!-- start:stateMachineIdeJs -->",
    //   endtag: "<!-- end:stateMachineIdeJs -->",
    //   transform: config.transformJsFun
    // }))
    .pipe($.inject(gulp.src(config.nodeData, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:node-data -->',
      endtag: '<!-- end:node-data -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.gis, {read: false, cwd: config.gisCommonJsDir}), {
      relative: true,
      starttag: '<!-- start:gis-commonJs -->',
      endtag: '<!-- end:gis-commonJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.config, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:configJs -->',
      endtag: '<!-- end:configJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.uwebUtil, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:uwebUtilJs -->',
      endtag: '<!-- end:uwebUtilJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.logicUniverseFor3D, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:logicUniverseFor3D -->',
      endtag: '<!-- end:logicUniverseFor3D -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.uwebHome, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:uwebHomeJs -->',
      endtag: '<!-- end:uwebHomeJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.headJs, {read: false, cwd: config.jsDir}), {
      relative: true,
      starttag: '<!-- start:headJs -->',
      endtag: '<!-- end:headJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.app, {read: false, cwd: config.appDir}), {
      relative: true,
      starttag: '<!-- start:appJs -->',
      endtag: '<!-- end:appJs -->',
      transform: config.transformJsFun
    }))
    .pipe($.inject(gulp.src(config.headCss, {read: false, cwd: config.cssDir}), {
      relative: true,
      starttag: '<!-- start:headCss -->',
      endtag: '<!-- end:headCss -->',
      transform: config.transformCssFun
    }))
    .pipe($.inject(gulp.src(config.css, {read: false, cwd: config.cssDir}), {
      relative: true,
      starttag: '<!-- start:pluginsCss -->',
      endtag: '<!-- end:pluginsCss -->',
      transform: config.transformCssFun
    }));
  if (config.isMinHtml) {
    temp = temp.pipe($.htmlmin(dataConfig.htmlMinOptions));
  }
  if (config.rename) {
    temp = temp.pipe($.rename(config.rename));
  }
  return temp.pipe(gulp.dest(config.destDir));
};

dist.transformJsFun = function (filePath, file, i, length) {
  // console.log(i, length);
  return getJsStr(filePath, ('../../' + distDir + '/').length);
};

dist.transformCssFun = function (filePath, file, i, length) {
  return getCssStr(filePath, ('../../' + distDir + '/').length);
};

dev.transformJsFun = function (filePath, file, i, length) {
  return getJsStr(filePath, '../dcv/'.length);
};

dev.transformCssFun = function (filePath, file, i, length) {
  return getCssStr(filePath, '../dcv/'.length);
};

dev.transformJsFun_stateMachine = function (filePath, file, i, length) {
  return getJsStr(filePath, '../dcv/javascripts/stateMachine/'.length);
};

dev.transformCssFun_stateMachine = function (filePath, file, i, length) {
  return getCssStr(filePath, '../dcv/javascripts/stateMachine/'.length);
};

/**
 * views目录下转换方法，html文件在views/XX/a.html
 */
dev.transformJsFun_views = function (filePath, file, i, length) {
  return getJsStr(filePath, '../dcv/'.length, '../../');
};

dev.transformCssFun_views = function (filePath, file, i, length) {
  return getCssStr(filePath, '../dcv/'.length, '../../');
};

dist.transformJsFun_views = function (filePath, file, i, length) {
  return getJsStr(filePath, ('../../' + distDir + '/').length, '../../');
};

dist.transformCssFun_views = function (filePath, file, i, length) {
  return getCssStr(filePath, ('../../' + distDir + '/').length, '../../');
};

// dist.transformJsFun_stateMachine = function (filePath, file, i, length) {
//   return getJsStr(filePath, "../../dist/javascripts/stateMachine/".length);
// };
//
// dist.transformCssFun_stateMachine = function (filePath, file, i, length) {
//   return getCssStr(filePath, "../../dist/javascripts/stateMachine/".length);
// };

/**
 * 压缩合并js
 * @author jw
 * @date 2016-11-08
 */
dist.concatJsFun = function (arr, name, cwd, dest) {
  cwd = cwd || dataConfig.dirs.dev;
  dest = dest || (distDir + '/js');
  return gulp.src(arr, {read: true, cwd: cwd})
    .pipe($.if(dataConfig.isMap, $.sourcemaps.init()))
    .pipe($.cache(
      $.uglify({
        compress: {
          drop_console: true, //删除console
          drop_debugger: false//忽略debugger
        }
      }), {
        fileCache: dataConfig.fileCache,
        name: 'concat'
      })
    )
    .pipe($.concat(name))
    .pipe($.if(dataConfig.isMap, $.sourcemaps.write(dataConfig.dirs.map)))
    .pipe(gulp.dest(dest));
};

/**
 * 压缩合并css
 * @author jw
 * @date 2016-11-08
 */
dist.concatCssFun = function (arr, name, cwd, dest) {
  cwd = cwd || dataConfig.dirs.dev;
  dest = dest || (distDir + '/css');
  return gulp.src(arr, {read: true, cwd: cwd})
    .pipe($.cache($.cleanCss(), {
      fileCache: dataConfig.fileCache,
      name: 'concat'
    }))
    .pipe($.concat(name))
    .pipe(gulp.dest(dest));
};

/**
 * 发布版本，替换引用的js/css到html
 * @param {String} html 要注入的Html名称
 * @param {Array} arr [合并的插件Js,合并的appJs]
 * @author jw
 * @date 2016-11-08
 */
dist.injectReleaseFun = function (html, arr, config) {
  var defaultConfig = {
    //js
    tool: ['tool.js', 'tool-brow.js'],
    t3d: ['t3d.js'],
    chromePlugin: 'chrome-plugin.js',
    t3dExJsArr: ['t3d-ex.js', 't3d-brow.js'],
    init: ['init.js', 'init-brow.js'],
    server: 'server.js',
    frontendConfigManager: 'frontendConfigManager.js',
    logicUniverse: 'logicUniverse-brow.js',
    threeD: '3d.js',
    u3d: 'u3d.js',
//    stateMachine: "stateMachine-eventLib.js",
    //状态机公用模块
    stateMachineCoreMinJs: 'stateMachineCore.min.js',
    stateMachineExtend: 'stateMachineExtend.min.js',

    config: ['config.js', 'config-brow.js'],
    uwebUtil: 'uwebUtil.js',
    uwebHome: 'uwebHome.js',
    logicUniverseFor3D: 'logicUniverseFor3D.js',
    gis: gisCommonJsArr,
    commonPlugins: browCommonPluginJsArr,
    plugins: arr[0],
    app: arr[1],
    headJs: 'head.js',
    nodeData: 'data_brow.js', //浏览器加载node代码列表
    //css
    css: arr[2],
    headCss: headCssArr,

    //目录
    pluginsJsDir: distDir,
    jsDir: (distDir + '/js'),
    stateMachineJsDir: (distDir + '/javascripts/stateMachine/minjs'),
    gisCommonJsDir: distDir,
    appDir: (distDir + '/js'),
    cssDir: distDir,
    destDir: distDir,

    //转换函数
    transformJsFun: dist.transformJsFun,
    transformCssFun: dist.transformCssFun,

    //是否压缩html
    isMinHtml: true
  };
  return injectFun(html, util.combine(defaultConfig, config));
};

/**
 * 3D可视化引用js替换注入
 * @author jw
 * @date 2016-11-08
 */
dev.injectImportFun = function (html, arr, config) {
  var dir = dataConfig.dirs.dev;
  var defaultConfig = {
    //js
    tool: toolJsArr.concat(dataConfig.dirs.browser.toolJsArr),
    t3d: t3dJsArr,
    chromePlugin: chromePluginJsArr,
    t3dExJsArr: t3dExJsArr.concat(dataConfig.dirs.browser.t3dJsArr),
    init: initJsArr.concat(dataConfig.dirs.browser.initJsArr),
    server: serverJsArr,
    frontendConfigManager: frontendConfigManagerJsArr,
    logicUniverse: dataConfig.dirs.browser.logicUniverseJsArr,
    threeD: threeDJsArr,
    u3d: u3dJsArr,
    config: configJsArr.concat(dataConfig.dirs.browser.configJsArr),
    uwebUtil: uwebUtilJsArr,
    uwebHome: uwebHomeJsArr,
    logicUniverseFor3D: logicUniverseFor3DJsArr,

    //状态机公用模块
    stateMachineCoreMinJs: stateMachineCoreMinJsArr,
    stateMachineExtend: stateMachineExtendJsArr,

    gis: gisCommonJsArr,
    commonPlugins: browCommonPluginJsArr,
    plugins: arr[0],
    app: arr[1],
    headJs: headJsArr,
    nodeData: dataConfig.dirs.browser.devData, //浏览器加载node代码列表
    //css
    css: arr[2],
    headCss: headCssArr,

    //目录
    pluginsJsDir: dir,
    jsDir: dir,
    appDir: dir,
    stateMachineJsDir: (dataConfig.dirs.dev + '/javascripts/stateMachine'),
    gisCommonJsDir: dir,
    cssDir: dir,
    destDir: dir,

    //转换函数
    transformJsFun: dev.transformJsFun,
    transformCssFun: dev.transformCssFun,

    //是否压缩html
    isMinHtml: false
  };
  return injectFun(html, util.combine(defaultConfig, config));
};

var injectDataNode = function (nodeDataArr, nodeDataArr2, path) {
  var data = "uinv.nodeDataArr = ['"
    + nodeDataArr.join("',\n'")
    + "'];\n"
    + "uinv.nodeDataArr2 = ['" + nodeDataArr2.join("',\n'")
    + "'];\n"; //前面是node端加载的
  return util.writeToFile(path, data);
};

/**
 * 注入node版本需要的js文件列表
 */
gulp.task('inject:data-node-dev', function () {
  var nodeDataArr = dataConfig.dirs.node.pluginJsArr
    .concat(dataConfig.dirs.common.toolJsArr)
    .concat(dataConfig.dirs.node.toolJsArr)
    .concat(dataConfig.dirs.common.t3dJsArr);
  var nodeDataArr2 = dataConfig.dirs.common.t3dExJsArr
    .concat(dataConfig.dirs.common.initJsArr)
    .concat(dataConfig.dirs.common.serverJsArr)
    .concat(dataConfig.dirs.node.frontendConfigManagerJsArr)
    .concat(dataConfig.dirs.node.configJsPreArr)
    .concat(dataConfig.dirs.node.threeDJsArr)
    .concat(dataConfig.dirs.common.logicUniverseJsArr)
    .concat(dataConfig.dirs.node.logicUniverseJsArr)
    .concat(dataConfig.dirs.node.stateMachineArr)
    .concat(dataConfig.dirs.common.u3dJsArr)
    .concat(dataConfig.dirs.node.u3dJsArr)
    .concat(dataConfig.dirs.node.uGeoJsArr)
    .concat(dataConfig.dirs.common.configJsArr)
    .concat(dataConfig.dirs.node.configJsArr)
    .concat(dataConfig.dirs.node.gameInitJsArr);

  return injectDataNode(nodeDataArr, nodeDataArr2, dataConfig.dirs.dev + '/' + dataConfig.dirs.node.devData);
});

/**
 * 发布版本：注入node版本需要的js文件列表
 */
gulp.task('inject:data-node-dist', function () {
  var nodeDataArr = dataConfig.dirs.node.pluginJsArr.concat(['js/tool.js', 'js/tool-node.js', 'js/t3d.js']);
  var nodeDataArr2 = ['js/t3d-ex.js', 'js/init.js', 'js/server.js', 'js/frontendConfigManager.js', 'js/userConfig.js', 'js/3d.js', 'js/u3d.js', 'js/u3d-node.js']
    .concat(dataConfig.dirs.common.logicUniverseJsArr)
    .concat('js/logicUniverse.js')
    .concat(dataConfig.dirs.node.stateMachineArr)
    .concat(dataConfig.dirs.node.uGeoJsArr)
    .concat(['js/config.js', 'js/config-node.js', 'js/gameInit.js']);
  return injectDataNode(nodeDataArr, nodeDataArr2, dataConfig.dirs.dist + '/' + dataConfig.dirs.node.distData);
});

/**
 * 注入portShow页面node版本需要的js文件列表
 */
gulp.task('inject-portShow:data-node-dev', function () {
  var nodeDataArr = dataConfig.dirs.node.pluginJsArr
    .concat(dataConfig.dirs.common.toolJsArr)
    .concat(dataConfig.dirs.node.toolJsArr)
    .concat(dataConfig.dirs.common.t3dJsArr);
  var nodeDataArr2 = dataConfig.dirs.common.t3dExJsArr
    .concat(dataConfig.dirs.common.initJsArr)
    .concat(dataConfig.dirs.common.serverJsArr)
    .concat(dataConfig.dirs.node.configJsPreArr)
    .concat(dataConfig.dirs.node.threeDJsArr)
    .concat(dataConfig.dirs.common.u3dJsArr)
    .concat(dataConfig.dirs.node.u3dJsArr)
    .concat(dataConfig.dirs.common.configJsArr)
    .concat(dataConfig.dirs.node.configJsArr)
    .concat(dataConfig.dirs.portShow.gameInitJsArr);
  return injectDataNode(nodeDataArr, nodeDataArr2, dataConfig.dirs.dev + '/' + dataConfig.dirs.portShow.devNodeData);
});

/**
 * 发布版本：注入portShow页面node版本需要的js文件列表
 */
gulp.task('inject-portShow:data-node-dist', function () {
  var nodeDataArr = dataConfig.dirs.node.pluginJsArr.concat(['js/tool.js', 'js/tool-node.js', 'js/t3d.js']);
  var nodeDataArr2 = ['js/t3d-ex.js', 'js/init.js', 'js/server.js', 'js/userConfig.js', 'js/3d.js', 'js/u3d.js', 'js/u3d-node.js']
    .concat(['js/config.js', 'js/config-node.js', 'js/portShow-gameInit.js']);
  return injectDataNode(nodeDataArr, nodeDataArr2, dataConfig.dirs.dist + '/' + dataConfig.dirs.portShow.distNodeData);
});

var injectDataBrow = function (nodeInitJsPath, nodeDataPath, browDataArr, frontendConfigManagerJsArr, path) {
  var data = "uinv.nodeInitJsPath = '"
    + (Array.isArray(nodeInitJsPath) ? nodeInitJsPath[0] : nodeInitJsPath)
    + "';\n"
    + "uinv.nodeDataPath = '"
    + nodeDataPath
    + "';\n"
    + "uinv.frontendConfigManagerJsArr = ['"
    + frontendConfigManagerJsArr.join("',\n'")
    + "'];\n"
    + "uinv.browDataArr = ['"
    + browDataArr.join("',\n'")
    + "'];\n";
  return util.writeToFile(path, data);
};

/**
 * 注入浏览器需要的js文件列表
 */
gulp.task('inject:data-brow-dev', function () {
  var browDataArr = dataConfig.dirs.node.configJsPreArr
    .concat(dataConfig.dirs.node.threeDJsArr)
    .concat(dataConfig.dirs.node.logicUniverseJsArr)
    .concat(dataConfig.dirs.node.stateMachineArr)
    .concat(dataConfig.dirs.node.u3dJsArr)
    .concat(dataConfig.dirs.browser.uGeoJsArr)
    .concat(dataConfig.dirs.node.configJsArr)
    .concat(dataConfig.dirs.node.gameInitJsArr);
  return injectDataBrow(dataConfig.dirs.node.nodeInitJsArr, dataConfig.dirs.node.devData, browDataArr, dataConfig.dirs.node.frontendConfigManagerJsArr, dataConfig.dirs.dev + '/' + dataConfig.dirs.browser.devData);
});

/**
 * 发布版本：注入浏览器版本需要的js文件列表
 */
gulp.task('inject:data-brow-dist', function () {
  var browDataArr = ['js/userConfig.js', 'js/3d.js', 'js/u3d-node.js']
    .concat('js/logicUniverse.js')
    .concat(dataConfig.dirs.node.stateMachineArr)
    .concat(dataConfig.dirs.browser.uGeoJsArr)
    .concat(['js/config.js', 'js/config-node.js', 'js/gameInit.js']);
  return injectDataBrow('js/nodeInit.js', dataConfig.dirs.node.distData, browDataArr, ['js/frontendConfigManager.js'], dataConfig.dirs.dist + '/' + dataConfig.dirs.browser.distData);
});

/**
 * 注入portShow页面浏览器需要的js文件列表
 */
gulp.task('inject-portShow:data-brow-dev', function () {
  var browDataArr = dataConfig.dirs.node.configJsPreArr
    .concat(dataConfig.dirs.node.threeDJsArr)
    .concat(dataConfig.dirs.node.u3dJsArr)
    .concat(dataConfig.dirs.node.configJsArr)
    .concat(dataConfig.dirs.portShow.gameInitJsArr);
  return injectDataBrow(dataConfig.dirs.node.nodeInitJsArr, dataConfig.dirs.portShow.devNodeData, browDataArr, dataConfig.dirs.node.frontendConfigManagerJsArr, dataConfig.dirs.dev + '/' + dataConfig.dirs.portShow.devBrowData);
});

/**
 * 发布版本：注入portShow页面浏览器版本需要的js文件列表
 */
gulp.task('inject-portShow:data-brow-dist', function () {
  var browDataArr = ['js/userConfig.js', 'js/3d.js', 'js/u3d-node.js']
    .concat(['js/config.js', 'js/config-node.js', 'js/portShow-gameInit.js']);
  return injectDataBrow('js/nodeInit.js', dataConfig.dirs.portShow.distNodeData, browDataArr, ['js/frontendConfigManager.js'], dataConfig.dirs.dist + '/' + dataConfig.dirs.portShow.distBrowData);
});

//注入页面
gulp.task('inject:home', function () {
  return dev.injectImportFun('home.html', [homePluginJsArr, homeAppJsArr, homePluginCssArr]);
});

gulp.task('inject:gis', function () {
  return dev.injectImportFun('gis.html', [gisPluginJsArr, gisAppJsArr, gisPluginCssArr]);
});

gulp.task('inject:3dConfig', function () {
  return dev.injectImportFun('configure.html', [threeDConfigPluginJsArr, threeDConfigAppJsArr, threeDConfigCssArr]);
});

//by lcj
gulp.task('inject:visibleconfig', function () {
  return dev.injectImportFun('visibleconfig.html', [threeDConfigPluginJsArr, threeDConfigAppJsArrForVisibleconfig, threeDConfigCssArr]);
});

gulp.task('inject:portShow', function () {
  var options = dev.getInjectViewsOptions('/views/portShow', {
    nodeData: dataConfig.dirs.portShow.devBrowData, //浏览器加载node代码列表
    rename: 'index.html'
  });
  return dev.injectImportFun('portShow.html', [portShowPluginsJsArr, portShowAppJsArr, portShowCssArr], options);
});

/**
 * 视角设置
 */
gulp.task('inject:viewPointSet', function () {
  var options = dev.getInjectViewsOptions('/views/viewPointSet', {
    rename: 'index.html'
  });
  return dev.injectImportFun('viewPointSet.html', [viewPointSetPluginsJsArr, viewPointSetAppJsArr, viewPointSetCssArr], options);
});

gulp.task('inject:list', function () {
  return dev.injectImportFun('list.html', [listPluginsJsArr, listAppJsArr, listPluginsCssArr]);
});

gulp.task('inject:blank', function () {
  return dev.injectImportFun('blank.html', [dataConfig.dirs.blank.pluginsJsArr, listAppJsArr, dataConfig.dirs.blank.cssArr]);//没有app.js，这里随便用一个，不然报错
});

gulp.task('inject:play', function () {
  var options = dev.getInjectViewsOptions('/views/play', {
    rename: 'index.html'
  });
  return dev.injectImportFun('play.html', [dataConfig.dirs.play.pluginsJsArr, dataConfig.dirs.play.appJsArr, dataConfig.dirs.blank.cssArr], options);//没有css，这里随便用一个，不然报错
});

gulp.task('inject:manager', function () {
  return dev.injectImportFun('manager.html', [managerPluginsJsArr, managerAppJsArr, managerPluginsCssArr]);
});

gulp.task('inject:resources', function () {
  return dev.injectImportFun('resources.html', [resourcesPluginsJsArr, resourcesAppJsArr, resourcesPluginsCssArr], {
    chromePlugin: dataConfig.dirs.browser.chromeForUserPluginJsArr
  });
});

gulp.task('inject:dcControl', function () {
  var options = dev.getInjectViewsOptions('/views/admin');
  return dev.injectImportFun('dcControl.html', [controlPluginsJsArr, controlAppJsArr, controlPluginsCssArr], options);
});

gulp.task('inject:givTMapping', function () {
  return dev.injectImportFun('givTMapping.html', [givmappingPluginsJsArr, givmappingAppJsArr, givmappingPluginsCssArr]);
});

gulp.task('inject:givList', function () {
  return dev.injectImportFun('givList.html', [givListPluginsJsArr, givListAppJsArr, givListPluginsCssArr]);
});

gulp.task('concat:initJs', function () {
  return dist.concatJsFun(initJsArr, 'init.js');
});

gulp.task('concat:initBrowJs', function () {
  var initJsArr = dataConfig.dirs.browser.initJsArr;
  var index = initJsArr.indexOf('javascripts/3d/_init_.js');
  initJsArr.splice(index, 0, '../entry/_dist_init_.js');//插入重写t3djs.basePath等路径
  return dist.concatJsFun(initJsArr, 'init-brow.js');
});

//合并3d
gulp.task('concat:3dJs', function () {
  return dist.concatJsFun(threeDJsArr, '3d.js');
});

gulp.task('concat:toolJs', function () {
  return dist.concatJsFun(toolJsArr, 'tool.js');
});

gulp.task('concat:toolBrowJs', function () {
  return dist.concatJsFun(dataConfig.dirs.browser.toolJsArr, 'tool-brow.js');
});

gulp.task('concat:toolNodeJs', function () {
  return dist.concatJsFun(dataConfig.dirs.node.toolJsArr, 'tool-node.js');
});

gulp.task('concat:t3dJs', function () {
  return dist.concatJsFun(t3dJsArr, 't3d.js');
});

gulp.task('concat:t3dExJs', function () {
  return dist.concatJsFun(dataConfig.dirs.common.t3dExJsArr, 't3d-ex.js');
});

gulp.task('concat:t3dBrowJs', function () {
  return dist.concatJsFun(dataConfig.dirs.browser.t3dJsArr, 't3d-brow.js');
});

gulp.task('concat:logicUniverseJs', function () {
  return dist.concatJsFun(logicUniverseJsArr, 'logicUniverse.js');
});

gulp.task('concat:logicUniverseBrowJs', function () {
  return dist.concatJsFun(dataConfig.dirs.browser.logicUniverseJsArr, 'logicUniverse-brow.js');
});

gulp.task('concat:u3dJs', function () {
  return dist.concatJsFun(u3dJsArr, 'u3d.js');
});

gulp.task('concat:u3dNodeJs', function () {
  return dist.concatJsFun(dataConfig.dirs.node.u3dJsArr, 'u3d-node.js');
});

gulp.task('concat:serverJs', function () {
  return dist.concatJsFun(serverJsArr, 'server.js');
});

gulp.task('concat:frontendConfigManagerJs', function () {
  return dist.concatJsFun(frontendConfigManagerJsArr, 'frontendConfigManager.js');
});

gulp.task('concat:configJs', function () {
  // return dist.concatJsFun(configJsArr,"config.js");
  //config.js暂时先不压缩
  var configPath = configJsArr.concat(dataConfig.dirs.browser.configJsArr).concat(dataConfig.dirs.node.configJsArr).concat(dataConfig.dirs.node.configJsPreArr);
  // console.log(configPath);
  return gulp.src(configPath, {cwd: dataConfig.dirs.dev})
  // .pipe($.concat("config.js"))
    .pipe(gulp.dest(distDir + '/js'));
});

gulp.task('concat:gameInit', function () {
  return dist.concatJsFun(dataConfig.dirs.node.gameInitJsArr, 'gameInit.js');
});

gulp.task('concat:nodeInit', function () {
  return dist.concatJsFun(dataConfig.dirs.node.nodeInitJsArr, 'nodeInit.js');
});

gulp.task('concat:logicUniverseFor3D', function () {
  return dist.concatJsFun(logicUniverseFor3DJsArr, 'logicUniverseFor3D.js');
});

gulp.task('concat:uwebUtilJs', function () {
  return dist.concatJsFun(uwebUtilJsArr, 'uwebUtil.js');
});

gulp.task('concat:uwebHomeJs', function () {
  return dist.concatJsFun(uwebHomeJsArr, 'uwebHome.js');
});

gulp.task('concat:head', function () {
  return dist.concatJsFun(headJsArr, 'head.js');
});

//公用的状态机js文件
// gulp.task("concat:home-pluginsJs", function () {
//   return dist.concatJsFun(homePluginJsArr,"home-plugins.js");
// });

gulp.task('concat:home-app', function () {
  return dist.concatJsFun(homeAppJsArr, 'home-app.js');
});

gulp.task('concat:chrome-plugin', function () {
  return dist.concatJsFun(chromePluginJsArr, 'chrome-plugin.js');
});

gulp.task('concat:chrome-for-user-plugin', function () {
  return dist.concatJsFun(dataConfig.dirs.browser.chromeForUserPluginJsArr, 'chrome-for-user-plugin.js');
});
// gulp.task("concat:home-css", function () {
//   return dist.concatCssFun(homePluginCssArr,"home.css");
// });

// gulp.task("concat:gis-pluginsJs", function () {
//   return dist.concatJsFun(gisPluginJsArr,"gis-plugins.js");
// });

gulp.task('concat:gis-app', function () {
  return dist.concatJsFun(gisAppJsArr, 'gis-app.js');
});

// gulp.task("concat:gis-css", function () {
//   return dist.concatCssFun(gisPluginCssArr,"gis.css");
// });

// gulp.task("concat:3dConfig-pluginsJs", function () {
//   return dist.concatJsFun(threeDConfigPluginJsArr,"3dConfig-plugins.js");
// });

gulp.task('concat:3dConfig-app', function () {
  return dist.concatJsFun(threeDConfigAppJsArr, '3dConfig-app.js');
});

//by lcj
gulp.task('concat:visibleconfig', function () {
  return dist.concatJsFun(threeDConfigAppJsArrForVisibleconfig, 'visibleconfig.js');
});

// gulp.task("concat:3dConfig-css", function () {
//   return dist.concatCssFun(threeDConfigCssArr,"3dConfig.css");
// });

// gulp.task("concat:list-pluginsJs", function () {
//   return dist.concatJsFun(listPluginsJsArr,"list-plugins.js");
// });

gulp.task('concat:play-app', function () {
  return dist.concatJsFun(dataConfig.dirs.play.appJsArr, 'play-app.js');
});

gulp.task('concat:list-app', function () {
  return dist.concatJsFun(listAppJsArr, 'list-app.js');
});

gulp.task('concat:manager-app', function () {
  return dist.concatJsFun(managerAppJsArr, 'manager-app.js');
});

gulp.task('concat:resources-app', function () {
  return dist.concatJsFun(resourcesAppJsArr, 'resources-app.js');
});

gulp.task('concat:dcControl-app', function () {
  return dist.concatJsFun(controlAppJsArr, 'dcControl-app.js');
});

gulp.task('concat:portShow-gameInit', function () {
  return dist.concatJsFun(dataConfig.dirs.portShow.gameInitJsArr, 'portShow-gameInit.js');
});

gulp.task('concat:portshow-app', function () {
  return dist.concatJsFun(portShowAppJsArr, 'portshow-app.js');
});

gulp.task('concat:viewPointSet-app', function () {
  return dist.concatJsFun(viewPointSetAppJsArr, 'viewPointSet-app.js');
});

gulp.task('concat:givList-app', function () {
  return dist.concatJsFun(givListAppJsArr, 'givList-app.js');
});

gulp.task('concat:givTMapping-app', function () {
  return dist.concatJsFun(givmappingAppJsArr, 'givTMapping-app.js');
});

// gulp.task("concat:list-css", function () {
//   return dist.concatCssFun(listPluginsCssArr,"list.css");
// });

gulp.task('inject:release-home', function () {
  return dist.injectReleaseFun('home.html', [homePluginJsArr, 'home-app.js', homePluginCssArr], {
    // stateMachine: stateMachineDistJsArr,
    // isMinHtml: false
  });
});

gulp.task('inject:release-gis', function () {
  return dist.injectReleaseFun('gis.html', [gisPluginJsArr, 'gis-app.js', gisPluginCssArr]);
});

gulp.task('inject:release-3dConfig', function () {
  return dist.injectReleaseFun('configure.html', [threeDConfigPluginJsArr, '3dConfig-app.js', threeDConfigCssArr]);
});

//by lcj
gulp.task('inject:release-visibleconfig', function () {
  return dist.injectReleaseFun('visibleconfig.html', [threeDConfigPluginJsArr, 'visibleconfig.js', threeDConfigCssArr]);
});

gulp.task('inject:release-list', function () {
  return dist.injectReleaseFun('list.html', [listPluginsJsArr, 'list-app.js', listPluginsCssArr]);
});

gulp.task('inject:release-play', function () {
  return dist.injectReleaseFun('play.html', [dataConfig.dirs.play.pluginsJsArr, 'play-app.js', dataConfig.dirs.blank.cssArr]);//随便用个css
});

gulp.task('inject:release-blank', function () {
  return dist.injectReleaseFun('blank.html', [dataConfig.dirs.blank.pluginsJsArr, 'list-app.js', dataConfig.dirs.blank.cssArr]);//随便用个app.js
});

gulp.task('inject:release-manager', function () {
  return dist.injectReleaseFun('manager.html', [managerPluginsJsArr, 'manager-app.js', managerPluginsCssArr]);
});

gulp.task('inject:release-resources', function () {
  return dist.injectReleaseFun('resources.html', [resourcesPluginsJsArr, 'resources-app.js', resourcesPluginsCssArr], {
    chromePlugin: 'chrome-for-user-plugin.js'
  });
});

gulp.task('inject:release-portShow', function () {
  var options = dist.getInjectViewsOptions('/views/portShow', {
    nodeData: 'portShow_data_brow.js', //浏览器加载node代码列表
    appDir: distDir + '/js',
    rename: 'index.html'
  });
  return dist.injectReleaseFun('portShow.html', [portShowPluginsJsArr, 'portshow-app.js', portShowCssArr], options);
});

gulp.task('inject:release-viewPointSet', function () {
  var options = dist.getInjectViewsOptions('/views/viewPointSet', {
    appDir: distDir + '/js',
    rename: 'index.html'
  });
  return dist.injectReleaseFun('viewPointSet.html', [viewPointSetPluginsJsArr, 'viewPointSet-app.js', viewPointSetCssArr], options);
});

gulp.task('inject:release-dcControl', function () {
  var options = dist.getInjectViewsOptions('/views/admin', {
    appDir: distDir + '/js'
  });
  return dist.injectReleaseFun('dcControl.html', [controlPluginsJsArr, 'dcControl-app.js', controlPluginsCssArr], options);
});

gulp.task('inject:release-givTMapping', function () {
  return dist.injectReleaseFun('givTMapping.html', [givmappingPluginsJsArr, 'givTMapping-app.js', givmappingPluginsCssArr]);
});

gulp.task('inject:release-givList', function () {
  return dist.injectReleaseFun('givList.html', [givListPluginsJsArr, 'givList-app.js', givListPluginsCssArr]);
});

gulp.task('concat-all',
  gulp.series(
    gulp.parallel(
      'concat:toolJs', 'concat:toolBrowJs', 'concat:toolNodeJs',
      'concat:t3dJs', 'concat:t3dBrowJs', 'concat:t3dExJs',
      'concat:initJs', 'concat:initBrowJs',
      'concat:serverJs', 'concat:frontendConfigManagerJs', 'concat:3dJs',
      'concat:u3dJs', 'concat:u3dNodeJs',
      'concat:configJs',
      'concat:uwebUtilJs', 'concat:uwebHomeJs',
      // "concat:home-pluginsJs",
      'concat:logicUniverseJs', 'concat:logicUniverseFor3D', 'concat:logicUniverseBrowJs',
      'concat:head',
      'concat:gameInit', 'concat:nodeInit',
      'concat:home-app',
      'concat:chrome-plugin', 'concat:chrome-for-user-plugin',
      // "concat:list-pluginsJs",
      'concat:play-app',
      'concat:list-app',
      'concat:manager-app',
      'concat:resources-app',
      'concat:dcControl-app',
      // "concat:gis-pluginsJs",
      // "concat:gis-app",
      'concat:portshow-app', 'concat:portShow-gameInit',
      'concat:viewPointSet-app',
      'concat:givList-app',
      'concat:givTMapping-app',
      // "concat:3dConfig-pluginsJs",
      'concat:3dConfig-app',
      'concat:visibleconfig'),
    gulp.parallel('inject:data-node-dist',
      'inject:data-brow-dist',
      'inject-portShow:data-node-dist',
      'inject-portShow:data-brow-dist'))
);

/**
 * 注入引用
 */
gulp.task('inject-all',
  gulp.parallel(
    'inject:data-node-dev', 'inject:data-brow-dev',
    'inject-portShow:data-node-dev', 'inject-portShow:data-brow-dev',
    'inject:home',
    'inject:list',
    'inject:manager',
    'inject:resources',
    'inject:dcControl',
    'inject:portShow',
    'inject:viewPointSet',
    'inject:gis',
    'inject:3dConfig',
    'inject:visibleconfig',
    'inject:givTMapping',
    'inject:givList',
    'inject:play',
    'inject:blank'
  )
);
/**
 * 注入状态机引用
 */
// gulp.task("inject-release-stateMachineExtend", );
/**
 * 注入引用
 * @author jw
 * @date 2016-11-14
 */
gulp.task('inject-release-all',
  gulp.parallel(
    'inject:release-home',
    'inject:release-list',
    'inject:release-manager',
    'inject:release-resources',
    'inject:release-dcControl',
    'inject:release-givTMapping',
    'inject:release-givList',
    'inject:release-portShow',
    'inject:release-viewPointSet',
    'inject:release-blank',
    'inject:release-play',
    // "inject:release-gis",
//    "inject:release-stateMachine",
    'inject:release-3dConfig',
    'inject:release-visibleconfig'
  )
);

/**
 * 发布版本，打包
 */
gulp.task('build',
  gulp.series(
    // "clear",
    'copy',
    gulp.parallel('css', 'htmlmin', 'script', 'concat-all'), //"image",
    gulp.parallel('inject-release-all', 'delEmptyDir')
  )
);

/**
 * 发布版本，打包并压缩
 */
// gulp.task("build-zip",
//   gulp.series(
//     "build",
//     gulp.parallel("zip", "zipMap")
//   )
// );

//测试版本，包括map文件
gulp.task('build-zip-all',
  gulp.series(
    'build',
    'zip-all'
  )
);

/**
 * 发布版本，打包，压缩，并清除dist文件夹
 */
// gulp.task("build-zip-clear",
//   gulp.series(
//     // "clear",
//     "build-zip",
//     "clear"
//   )
// );
